package leetcode.code0405;

public class Solution1 extends Solution405 {

	public String toHexstupid(int num) {
		if (num == 0)
			return "0";
		int n = 8;
		int mask = 0b1111;
		char[] ans = new char[n];
		boolean b = num > 0 ? true : false;
		while (n != 0) {
			if (b && num == 0) {
				break;
			}
			ans[--n] = digits[num & mask];
			num >>>= 4;
		}
		return new String(ans, n, 8 - n);
	}

	@Override
	public String toHex(int num) {
		if (num == 0)
			return "0";
		int n = 8;
		int mask = 0b1111;
		char[] ans = new char[n];
		while (num != 0) {
			ans[--n] = digits[num & mask];
			num >>>= 4;
		}
		return new String(ans, n, 8 - n);
	}

	final char[] digits = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};


    public String toHex1(int num) {
        if (num == 0) return "0";
        StringBuilder sb = new StringBuilder();
        while (num != 0) {
            int u = num & 15;
            char c = (char)(u + '0');
            if (u >= 10) c = (char)(u - 10 + 'a');
            sb.append(c);
            num >>>= 4;
        }
        return sb.reverse().toString();
    }


	public static void main(String[] args) {
		Solution1 so = new Solution1();
		so.debug1();
		so.debug2();
		so.debug3();
		so.debug4();
		String r = so.toHex1(26);
		System.out.println(r);
//		System.out.println(Integer.toBinaryString(-1 & 0x1111));

	}

}
